package cn.stard.pet.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.pet.service.IPetTypeService;
import cn.stard.pet.domain.PetType;
import cn.stard.pet.query.PetTypeQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name="宠物类型添加修改", sn = "petType:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null)
                petTypeService.update(petType);
            else
                petTypeService.save(petType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name="宠物类型删除", sn = "petType:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name="宠物类型根据id获取数据", sn = "petType:get")
    public PetType get(@PathVariable("id")Long id)
    {
        return petTypeService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name="宠物类型展示数据", sn = "petType:list")
    public List<PetType> list(){

        return petTypeService.loadAll();
    }


    @GetMapping("/tree")// 请求地址：http://localhost:8080/department GET请求
    public List<PetType> tree(){
        return petTypeService.tree();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name="宠物类型展示数据分页", sn = "petType:json")
    public PageList<PetType> json(@RequestBody PetTypeQuery query)
    {
        return petTypeService.queryData(query);
    }
}
